import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.io.BufferedReader;
import java.io.FileReader;

/**
 * This class makes a list of adjacency of every vertex
 * @author Hyangjin Lee
 *
 */
public class AdjacencyList {
	int lineno = 0;
	String line = null;
	/**
	 * This is a map (Hash Table) of vertices to their adjacent vertices and the weight associated with that edge
	 */
	private Map<Vertex, Map<Vertex, Integer>> adjList = new HashMap<Vertex, Map<Vertex, Integer>>();
	

	/**
	 * Constructs AdjacencyLIst by reading in a given text file that contains the edges
	 * @param txtFile The text format file that contains the edges
	 * @throws IOException
	 */
	public AdjacencyList(File txtFile) throws IOException {
		BufferedReader br = new BufferedReader(new FileReader(txtFile));
		line = br.readLine();
		
		while(line != null)
		{
			String splitarray[] = line.split(" ");
			Vertex U = new Vertex(splitarray[3*lineno]);
			Vertex V = new Vertex(splitarray[3*lineno+1]);
			Integer weight = Integer.parseInt(splitarray[3*lineno+2]);
			if(!adjList.containsKey(U))
			{
				adjList.put(U, new HashMap<Vertex, Integer>());
			}
			adjList.get(U).put(V, weight);

			if(!adjList.containsKey(V))
			{
				adjList.put(V, new HashMap<Vertex, Integer>());
			}
			adjList.get(V).put(U, weight);

			line = br.readLine();
		}
	}

	@Override
	public String toString() {
		return "" + adjList.entrySet();
	}
	
	/**
	 * This returns the adjacency list
	 * @return the adjacency list of the given graph
	 */
	public Map<Vertex, Map<Vertex, Integer>> getAdjList() {
		return adjList;
	}
}
